package com.changsideal.excel.util;

import com.changsideal.core.MainBean;
import com.changsideal.exception.ExcelToolException;
import lombok.NoArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AbstractExcelBasicTool
 * @Description 只做基本Excel相关工具操作类
 * @Author Create by Chansideal.Haosc
 * @Date 2019/7/8 14:00
 * @Version 1.0
 **/
public class ExcelBasicTool implements MainBean {

    protected Workbook workbook;

    public ExcelBasicTool(){
        workbook = new HSSFWorkbook();
    }

    /**
     * 构造注入
     *
     * @param workbook 注入类型
     */
    public ExcelBasicTool(Workbook workbook) {
        this.workbook = workbook;
    }

    /**
     * 构造注入
     *
     * @param inputStream 注入类型
     */
    public ExcelBasicTool(InputStream inputStream) {
        try {
            this.workbook = WorkbookFactory.create(inputStream);
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
    }

    /**
     * 构造注入
     *
     * @param file 注入类型
     */
    public ExcelBasicTool(File file) {
        try {
            this.workbook = WorkbookFactory.create(file);
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
    }

    protected List<List<String>> read() throws ExcelToolException {
        return read(0, getRowCount(0) - 1);
    }

    public List<List<String>> read(Integer sheetIx) throws ExcelToolException {
        return read(sheetIx, getRowCount(sheetIx) - 1);
    }

    private List<List<String>> read(int sheetIx, int end) throws ExcelToolException {
        init();
        Sheet sheet = workbook.getSheetAt(sheetIx);
        List<List<String>> list = new ArrayList<>();

        if (end > getRowCount(sheetIx)) {
            end = getRowCount(sheetIx);
        }

        int cols = sheet.getRow(0).getLastCellNum();

        for (int i = 0; i <= end; i++) {
            List<String> rowList = new ArrayList<>();
            Row row = sheet.getRow(i);
            for (int j = 0; j < cols; j++) {
                if (row == null) {
                    rowList.add(null);
                    continue;
                }
                rowList.add(getCellValueToString(row.getCell((short) j)));
            }
            list.add(rowList);
        }
        return list;
    }

    private String getCellValueToString(Cell cell) {
        String strCell = "";
        if (cell == null) {
            return null;
        }
        switch (cell.getCellTypeEnum()) {
            case BOOLEAN:
                strCell = String.valueOf(cell.getBooleanCellValue());
                break;
            case NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Date date = cell.getDateCellValue();
                    strCell = date.toString();
                    break;
                }
                strCell = String.valueOf(cell.getNumericCellValue());
                break;
            case STRING:
                strCell = cell.getStringCellValue();
                break;
            default:
                break;
        }
        return strCell;
    }

    protected Integer getRowCount(int sheetIx) {
        init();
        Sheet sheet = workbook.getSheetAt(sheetIx);
        if (sheet.getPhysicalNumberOfRows() == 0) {
            return 0;
        }
        return sheet.getLastRowNum() + 1;
    }

    protected List<String> getRowValue(int sheetIx, int rowIndex) {
        init();
        Sheet sheet = workbook.getSheetAt(sheetIx);
        Row row = sheet.getRow(rowIndex);
        List<String> list = new ArrayList<>(10);
        if (row == null) {
            list.add(null);
        } else {
            for (int i = 0; i < row.getLastCellNum(); i++) {
                list.add(getCellValueToString(row.getCell((short) i)));
            }
        }
        return list;
    }

    public void createHead() {
//        workbook.cr
    }

    @Override
    public void init() {
        if (null == workbook) {
            throw new ExcelToolException("workbook configuration is null!");
        }
    }

    @Override
    public void close() {
        try {
            if (null != workbook) {
                workbook.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
